/*
 * Web Crawler
 * Date: November 01, 2011
 * Group JEDi
 * Author(s): James Lovato, Efim Todorasco, Daniel Garrison
 */
package singlethread;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * Class that creates a document from a web page's contents.
 * @author James Lovato, Efim Todorasco, Daniel Garrison
 * @version 10/20/11
 */
public class CrawlerPageRetriever {	
	
	/**
	 * The time from when the first page was about to be parsed.
	 */
	public long start_time;
	
	/**
	 * The total time took to crawl through the page.
	 */
	public long total_time;
	
	/**
	 * The length of the longest connection held.
	 */
	public long longest_connection;

	/**
	 * A list that stores the IOExceptions thrown during the crawling process.
	 */
	public List<IOException> my_io_exceptions = new ArrayList<IOException>();
	
	/**
	 * A list that stores the UnsupportedCharsetException thrown during the crawling process.
	 */
	public List<UnsupportedCharsetException> my_charset_exceptions = new ArrayList<UnsupportedCharsetException>();
	
	/**
	 * A list that stores the IllegalArgumentException thrown during the crawling process.
	 */	
	public List<IllegalArgumentException> my_ill_arg_exceptions = new ArrayList<IllegalArgumentException>();
	
	/**
	 * Creates a document from a jsoup connection.
	 * Connection/read timeout set to 3000 based on empirical data gather over several trials
	 * using different timeout setting and comparing the total time to retrieve 50 pages and
	 * the overall connection/read timeout errors. (Dan posted trials to the wiki).
	 * @param the_link String representation of the web link to be retrieved.
	 * @return What is the document created from the input string?
	 * @throws IOException
	 */
	public Document pageRetriever(String the_link) throws IOException {
		Document document = null;
		try {
			start_time = System.nanoTime();
			document = Jsoup.connect(the_link).timeout(3000).get();
			total_time = System.nanoTime() - start_time;
			if (total_time > longest_connection) {
				longest_connection = total_time; 
			}
		} catch (IOException e) {
			my_io_exceptions.add(e);
		} catch (UnsupportedCharsetException e) {
			my_charset_exceptions.add(e);
		} catch (IllegalArgumentException e) {
			my_ill_arg_exceptions.add(e);
		}
		return document;
	}
}